//
//  KFDPreferentialActivitiesViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 6/30/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDPreferentialActivitiesViewController.h"
#import "KFDUIConfigurator.h"
#import "KFDPreferentialActivitiesNavBarTabControl.h"
#import "KFDPreferentialActivitiesViewModel.h"
#import "KFDPreferentialActivitiesViewCell.h"
#import "KFDPreferentialActivityViewController.h"
#import <MJRefresh/MJRefresh.h>
#import "JTToast.h"

#define KFDPreferentialActivitiesViewCellReuseIdentifier @"KFDPreferentialActivitiesViewCell"
#define KFDPreferentialActivitiesViewCellNibName @"KFDPreferentialActivitiesViewCell"

@interface KFDPreferentialActivitiesViewController ()

@property (strong,nonatomic) KFDPreferentialActivitiesViewModel* viewModel;
@property (assign,nonatomic) NSInteger type;

@end

@implementation KFDPreferentialActivitiesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.viewModel=[[KFDPreferentialActivitiesViewModel alloc] init];
    self.type=1;
    KFDPreferentialActivitiesNavBarTabControl* titleView=[[KFDPreferentialActivitiesNavBarTabControl alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    [titleView setDidSelectTypeAction:^(NSInteger type) {
        self.type=type;
        if (type==1) {
            [self.viewModel loadActivitiesUnderwayWithMaxCount:20 page:1];
        }else if(type==2){
            [self.viewModel loadActivitiesFinishedWithMaxCount:20 page:1];
        }
    }];
    self.navigationItem.titleView=titleView;
    [self addObserver];
    [self.viewModel loadActivitiesUnderwayWithMaxCount:20 page:1];
    [self setupRefreshing];
}

-(void)setupRefreshing
{
    __weak typeof(self) weakSelf=self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        if (self.type==1) {
            [weakSelf.viewModel loadActivitiesUnderwayWithMaxCount:20 page:1];
        }else if (self.type==2){
            [weakSelf.viewModel loadActivitiesFinishedWithMaxCount:20 page:1];
        }
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        if (self.type==1) {
            [weakSelf.viewModel loadActivitiesUnderwayWithMaxCount:weakSelf.viewModel.activitiesUnderway.count+10 page:1];
        }else if (self.type==2){
            [weakSelf.viewModel loadActivitiesFinishedWithMaxCount:weakSelf.viewModel.activitiesFinished.count+10 page:1];
        }
    }];
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addObserver
{
    NSNotificationCenter* notificationCenter=[NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(activitiesUnderwayReady) name:KFDPreferentialActivitiesUnderwayReadyNotification object:nil];
    [notificationCenter addObserver:self selector:@selector(activitiesFinishedReady) name:KFDPreferentialActivitiesFinishedReadyNotification object:nil];
}

-(void)dealloc
{
    NSNotificationCenter* notificationCenter=[NSNotificationCenter defaultCenter];
    [notificationCenter removeObserver:self name:KFDPreferentialActivitiesUnderwayReadyNotification object:nil];
    [notificationCenter removeObserver:self name:KFDPreferentialActivitiesFinishedReadyNotification object:nil];
}

-(void)activitiesUnderwayReady
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([self.tableView.header isRefreshing]) {
            [self.tableView.header endRefreshing];
        }
        if ([self.tableView.footer isRefreshing]) {
            [self.tableView.footer endRefreshing];
        }
        [self.tableView reloadData];
    });
}

-(void)activitiesFinishedReady
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([self.tableView.header isRefreshing]) {
            [self.tableView.header endRefreshing];
        }
        if ([self.tableView.footer isRefreshing]) {
            [self.tableView.footer endRefreshing];
        }
        [self.tableView reloadData];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.type==1) {
        return self.viewModel.activitiesUnderway.count;
    }else if(self.type==2){
        return self.viewModel.activitiesFinished.count;
    }
    return 0;
}

-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return 101;
}

-(UITableViewCell*)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    KFDPreferentialActivitiesViewCell* cell=[tableView dequeueReusableCellWithIdentifier:KFDPreferentialActivitiesViewCellReuseIdentifier];
    if (!cell) {
        cell=[[NSBundle mainBundle] loadNibNamed:KFDPreferentialActivitiesViewCellNibName owner:nil options:nil].firstObject;
    }
    if (self.type==1) {
        [cell setActivity:self.viewModel.activitiesUnderway[indexPath.row]];
        cell.joinButtonAction=^(BOOL success,NSString* message){
            if (success) {
                UIAlertController* alertController=[UIAlertController alertControllerWithTitle:nil message:@"参加成功，已收藏到我的活动列表" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction* ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                [alertController addAction:ok];
                [self presentViewController:alertController animated:YES completion:nil];
            }else{
                [[JTToast makeText:message] showInView:self.view size:CGSizeMake(message.length*20, 40)];
            }
        };
    }else if(self.type==2){
        [cell setActivity:self.viewModel.activitiesFinished[indexPath.row]];
    }
    return cell;
}


#pragma mark - Table view delegate

-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KFDPreferentialActivityViewController* preferentialActivityVC=[[KFDPreferentialActivityViewController alloc] init];
    KFDPreferentialActivity* activity=nil;
    if (self.type==1) {
        activity=self.viewModel.activitiesUnderway[indexPath.row];
    }else if(self.type==2){
        activity=self.viewModel.activitiesFinished[indexPath.row];
    }
    preferentialActivityVC.url=activity.url;
    [self.navigationController pushViewController:preferentialActivityVC animated:YES];
}

@end
